home *** CD-ROM | disk | FTP | other *** search
- /*
- * ThreadedSnakesMain.c
- *
- * Author: Brad Post
- * Creation Date: 12/16/92
- * Copyright © 1992, 1993 Apple Computer Inc.
- *
- * This file is the main for ThreadedSnakes. It just creates the window, handles the user events and
- * simple stuff.
- */
-
- #include "ThreadedSnakes.h"
-
- VEC theBounds; // bounds to which the snakes can be draw into
- MenuHandle myMenus[2]; // menu handles
- WindowPtr theWindow; // window to draw the snakes in
- WindowRecord theWindowRec; // windowrec for the window we draw snakes in
-
- /*
- * myError
- *
- * This function just takes in a Str255 and pops up an ALRT box saying what's wrong, and then punts
- * away.
- */
- myError(Str255 theString)
- {
- ParamText(theString, NULL, NULL, NULL);
- Alert(errorALRT, NULL);
- ExitToShell();
- }
-
- /*
- * InitAll
- *
- * Basic Initialization routine. Takes care of creating the window to draw the snakes in as well as setting up
- * the menus.
- */
- InitAll()
- {
- Rect theRect;
-
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- /* Set up the menus. */
- myMenus[1] = GetMenu(fileMenuID);
- InsertMenu(myMenus[0] = NewMenu(1, "\p\024"), 0);
- InsertMenu(myMenus[1], 0);
- AppendMenu(myMenus[0], "\pAbout SnakesWSemaphores;(-");
- AddResMenu(myMenus[0], 'DRVR');
- DrawMenuBar();
-
- /* Determine the size of the screen */
- theRect = qd.screenBits.bounds;
-
- theRect.top += (2 * MBarHeight);
- theRect.left += (2 * MBarHeight);
- theRect.right -= (2 * MBarHeight);
- theRect.bottom -= (2 * MBarHeight);
-
- /* Create a window to draw the snakes in, bounded by the rectangle 'theRect'. */
-
- theWindow = NewWindow(&(theWindowRec), &theRect, "\p", 1, noGrowDocProc, (WindowPtr) -1, 0, 0);
- ShowWindow(theWindow);
- SetPort(theWindow);
-
- /* To determine where the snake can move to, set these bounds. */
- theBounds[0] = theRect.bottom; // for testing the V of a point
- theBounds[1] = theRect.right; // for testing the H of a point
-
- /* Make the necessary initialization calls */
- InitializeThreadUtilities();
- initSnakes();
-
- }
-
-
- /*
- * IsAppWindow
- *
- * Returns true is the window is the app window, otherwise false.
- */
- Boolean IsAppWindow(window)
- WindowPtr window;
- {
- short windowKind;
-
- if ( window == nil )
- return false;
- else { /* application windows have windowKinds = userKind (8) */
- windowKind = ((WindowPeek) window)->windowKind;
- return ( windowKind == userKind );
- }
- }
-
- /*
- * DoUpdate
- *
- * Handles update events.
- */
- void DoUpdate(window)
- WindowPtr window;
- {
- if ( IsAppWindow(window) ) {
- BeginUpdate(window); /* this sets up the visRgn */
- if ( ! EmptyRgn(window->visRgn) ) /* draw if updating needs to be done */
- ;
- EndUpdate(window);
- }
- }
-
- /*
- * DoActivate
- *
- * Handles activate events.
- */
- void DoActivate(window, becomingActive)
- WindowPtr window;
- Boolean becomingActive;
- {
- if ( IsAppWindow(window) ) {
- if ( becomingActive )
- /* do whatever you need to at activation */ ;
- else
- /* do whatever you need to at deactivation */ ;
- }
- }
-
- /*
- * doMenus
- *
- * This function handles all the menu stuff.
- */
- short doMenus(long selector)
- {
- short whichMenu = HiWord(selector);
- short whichItem = LoWord(selector);
- GrafPtr savePort;
- Str255 name;
-
- switch(whichMenu)
- {
- case appleMenuID:
- switch(whichItem)
- {
- case 1: Alert(aboutALRT, 0L);
- return notDONE;
- default:
- {
- GetPort(&savePort);
- GetItem(myMenus[0], whichItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- return notDONE;
- }
- }
- break;
- case fileMenuID:
- switch(whichItem)
- {
- default:
- return notDONE;
- case fmQuit:
- cleanUpSnakes();
- ExitToShell();
- return areDONE;
- }
- break;
- }
-
- HiliteMenu(0);
- return notDONE;
-
- }
-
-
- /*
- * main
- *
- * The main function that just handles the event loop and yeilds to any thread.
- * The event loop is in a critical section so that no preemptive thread (which are using quickdraw)
- * can interrupt us and draw through out menu bar and such.
- */
- main()
- {
- short done = 0;
- short whichWindow;
- WindowPtr theWindow;
- EventRecord myEvent;
- Point aPoint;
-
- InitAll();
-
- while( !done )
- {
- ThreadBeginCritical();
-
- SystemTask();
- if(GetNextEvent(everyEvent, &myEvent) == true)
- {
- switch(myEvent.what)
- {
- case mouseDown:
- {
- whichWindow = FindWindow(myEvent.where, &theWindow);
- switch(whichWindow)
- {
- case inSysWindow:
- SystemClick(&myEvent, theWindow);
- break;
- case inMenuBar:
- done = doMenus(MenuSelect(myEvent.where));
- break;
- case inDrag:
- DragWindow( theWindow, myEvent.where, &qd.screenBits.bounds);
- break;
- }
- }
- break;
- case keyDown:
- case autoKey:
- if((myEvent.modifiers & cmdKey) != 0)
- done = doMenus(MenuKey( (char) (myEvent.message & charCodeMask)));
- break;
- case activateEvt:
- DoActivate((WindowPtr) myEvent.message, (myEvent.modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) myEvent.message);
- break;
- case diskEvt:
- if ( HiWord(myEvent.message) != noErr ) {
- SetPt(&aPoint, kDILeft, kDITop);
- DIBadMount(aPoint, myEvent.message);
- }
- break;
- }
- }
-
- ThreadEndCritical();
-
- if(YieldToAnyThread() != noErr)
- DebugStr("\pGot an error on YieldToAnyThread in Main Thread");
- }
- }
-